/*
 * Copyright (C) 2014 Will Dignazio
 * workgroup.c
 */
#include <Python.h>
#include <inttypes.h>

#include <e-hal.h>
#include <pyepiphany.h>
#include "internal.h"

/* Epiphany Workgroup Wrapper Object */
struct EpiphanyWorkgroup
{
	PyObject_HEAD
	e_epiphany_t	device;		// eCore Workgroup 
};

/* Python exposed members */
static EpiphanyMember workgroup_members[] = 
{
	{ NULL },
};

/*
 * Similiar to the workgroup object in the eSDK, I wish
 * to have a uniform object with which to contain a 
 * Workgroup. This type extension shall wrap around the
 * e_epiphany_t and serve ad the interface to its properties.
 */
PyTypeObject workgroup_type = 
{
	PyObject_HEAD_INIT(NULL)
	0,				/* ob_size */
	"epiphany.Workgroup",		/* tp_name */
	sizeof(EpiphanyWorkgroup),	/* tp_basicsize */
	0,				/* tp_itemsize */
	0,				/* tp_dealloc */
	0,				/* tp_print */
	0,                         	/* tp_getattr */
	0,                         	/* tp_setattr*/
	0,                         	/* tp_compare */
	0,                         	/* tp_repr */
	0,                         	/* tp_as_number */
	0,                         	/* tp_as_sequence */
	0,                         	/* tp_as_mapping */
	0,                         	/* tp_hash */
	0,                         	/* tp_call */
	0,                         	/* tp_str */
	0,                         	/* tp_getattro */
	0,                         	/* tp_setattro */
	0,                         	/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
	"Epiphany objects",           	/* tp_doc */
	0,               		/* tp_traverse */
	0,               		/* tp_clear */
	0,               		/* tp_richcompare */
	0,               		/* tp_weaklistoffset */
	0,               		/* tp_iter */
	0,               		/* tp_iternext */
	0,		             	/* tp_methods */
	workgroup_members,             	/* tp_members */
	0,                         	/* tp_getset */
	0,                         	/* tp_base */
	0,                         	/* tp_dict */
	0,                         	/* tp_descr_get */
	0,                         	/* tp_descr_set */
	0,                         	/* tp_dictoffset */
	0,			 	/* tp_init */
	0,                         	/* tp_alloc */
	workgroup_type_new,           	/* tp_new */
};

/* new obj callback, called before init */
PyObject*
workgroup_type_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
	EpiphanyWorkgroup *wg;
	wg = (EpiphanyWorkgroup*)type->tp_alloc(type, 0);
	if(type == NULL)
		return NULL;

	return (PyObject*)wg;
}

/*
 * XXX: Does NOT initialize a new Workgroup objct
 * This serves as quick way to initialize the workgroup
 * section of the whole extension. This should only be 
 * run from epiphany.c
 */
void
initworkgroup(PyObject* parent)
{
	workgroup_type.tp_new = PyType_GenericNew;
	if(PyType_Ready(&workgroup_type) < 0)
		return;

	Py_INCREF(&workgroup_type);
	PyModule_AddObject(parent, "Workgroup", (PyObject*)&workgroup_type);
}

/* Defines a *valid* eCore workgroup */
PyObject*
epiphany_open(PyObject *self, PyObject *args)
{
	EpiphanyWorkgroup *wg;
	unsigned rows, cols;
	unsigned row, col;
	int ret;

	ret = 0;

	if(!PyArg_ParseTuple(args, "OIIII", &wg, 
			     &row, &col, 
			     &rows, &cols)) {
		return NULL;
	}

	ret = e_open(&wg->device, row, col, rows, cols);
	if(ret != E_OK)
		ret = -1;

	Py_INCREF(wg); // We need to keep it around until e_close
	return Py_BuildValue("i", ret);
}
